package com.qf.service.impl;

import com.qf.constans.ResultCode;
import com.qf.dto.UserDto;
import com.qf.entity.CheckType;
import com.qf.entity.TAdminUser;
import com.qf.exceptions.QueryDataException;
import com.qf.mapper.UserMapper;
import com.qf.service.UserService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/*
杨威
2021/10/26
20:54
*/
@Service
public class UserServiceImpl implements UserService {
    private UserMapper userMapper;
    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public TAdminUser login(UserDto userDto) {
        System.out.println(111);
        if(userDto.isNull(CheckType.ADD)){
            throw new QueryDataException("用户名或密码错误2", ResultCode.USERNAME_OR_PASSWORD_ERROR);
        }
        //不抛异常的话正常执行查询
        TAdminUser user = userMapper.selectUserByUsercode(userDto.getUsercode());
        //将传递的密码转成md5
        String password = userDto.getPassword();
        String md5password=DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        String md5pwd = user.getPassword();
        if(!md5pwd.equalsIgnoreCase(md5password)){
            throw new QueryDataException("用户名或密码错误", ResultCode.USERNAME_OR_PASSWORD_ERROR);
        }

        return user;
    }
}
